package com.fw.payment.bao;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.payment.service.ServiceExecutor;
import com.fw.payment.service.ServiceOrder;
import com.fw.service.captcha.CaptchaService;

@Component
@Scope(SCOPE_PROTOTYPE)
public class CaptchaBAO extends AbstractBAO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private CaptchaService captchaService;

    public byte[] loadCaptcha(String sessionId, Locale locale) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.captchaService);
	    serviceOrder.setMethodName("loadCaptcha");
	    serviceOrder.setParameterTypes(new Class[] { String.class, Locale.class });
	    serviceOrder.setParameterValues(new Object[] { sessionId, locale });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		byte[] bs = (byte[]) serviceOrder.getReturnValue();
		return bs;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public Boolean validateCaptcha(String sessionId, String captcha) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.captchaService);
	    serviceOrder.setMethodName("validateCaptcha");
	    serviceOrder.setParameterTypes(new Class[] { String.class, String.class });
	    serviceOrder.setParameterValues(new Object[] { sessionId, captcha });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		Boolean result = (Boolean) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

}
